import $ from 'jquery';

export class JqScrollStopService {
    special;
    uid1;
    uid2;

    init() {
        this.special = $['event'].special;
        this.uid1 = 'D' + (+new Date());
        this.uid2 = 'D' + (+new Date() + 1);

        this.special.scrollstart = {
            setup: function () {
                let timer,
                    handler = function (evt) {
                        let _self = this;
                        if (timer) {
                            clearTimeout(timer);
                        } else {
                            evt.type = 'scrollstart';
                            $['event'].handle.apply(_self, arguments);
                        }
                        timer = setTimeout(function () {
                            timer = null;
                        }, this.special.scrollstop.latency);
                    };

                $(this).bind('scroll', handler).data(this.uid1, handler);
            },
            teardown: function () {
                $(this).unbind('scroll', () => {
                    $(this).data(this.uid1)
                });
            }
        };

        const globalSelf = this;
        this.special.scrollstop = {
            latency: 300,
            setup: function () {

                let timer,
                    handler = function (evt) {

                        let _self = this,
                            _args = arguments;

                        if (timer) {
                            clearTimeout(timer);
                        }

                        timer = setTimeout(function () {

                            timer = null;
                            evt.type = 'scrollstop';
                            console.log('$',$);
                            console.log('$.event',$['event']);
                            console.log('$[event].handle',$['event'].handle);
                            // $['event'].handle.apply(_self, _args);

                        }, globalSelf.special.scrollstop.latency);

                    };

                $(this).bind('scroll', handler).data(this.uid2, handler);

            },
            teardown: function () {
                $(this).unbind('scroll', () => {
                    $(this).data(this.uid2);
                });
            }
        };
    }

}
